home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / cawf404.zip / nreq.c < prev    next >
C/C++ Source or Header  |  1993-12-07  |  21KB  |  974 lines

  1. /*
  2.  *    nreq.c - cawf(1) processing of nroff requests
  3.  */
  4.  
  5. /*
  6.  *    Copyright (c) 1991 Purdue University Research Foundation,
  7.  *    West Lafayette, Indiana 47907.  All rights reserved.
  8.  *
  9.  *    Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
  10.  *    University Computing Center.  Not derived from licensed software;
  11.  *    derived from awf(1) by Henry Spencer of the University of Toronto.
  12.  *
  13.  *    Permission is granted to anyone to use this software for any
  14.  *    purpose on any computer system, and to alter it and redistribute
  15.  *    it freely, subject to the following restrictions:
  16.  *
  17.  *    1. The author is not responsible for any consequences of use of
  18.  *       this software, even if they arise from flaws in it.
  19.  *
  20.  *    2. The origin of this software must not be misrepresented, either
  21.  *       by explicit claim or by omission.  Credits must appear in the
  22.  *       documentation.
  23.  *
  24.  *    3. Altered versions must be plainly marked as such, and must not
  25.  *       be misrepresented as being the original software.  Credits must
  26.  *       appear in the documentation.
  27.  *
  28.  *    4. This notice may not be removed or altered.
  29.  */
  30.  
  31. #include "cawf.h"
  32. #include <ctype.h>
  33.  
  34.  
  35. /*
  36.  * Prototypes for request processing functions.
  37.  */
  38.  
  39. _PROTOTYPE(static void nr_UL,(unsigned char *line, int brk));
  40. _PROTOTYPE(static void nr_Ub,(unsigned char *line, int brk));
  41. _PROTOTYPE(static void nr_Uc,(unsigned char *line, int brk));
  42. _PROTOTYPE(static void nr_Uf,(unsigned char *line, int brk));
  43. _PROTOTYPE(static void nr_Ur,(unsigned char *line, int brk));
  44. _PROTOTYPE(static void nr_ad,(unsigned char *line, int brk));
  45. _PROTOTYPE(static void nr_bp,(unsigned char *line, int brk));
  46. _PROTOTYPE(static void nr_br,(unsigned char *line, int brk));
  47. _PROTOTYPE(static void nr_ce,(unsigned char *line, int brk));
  48. _PROTOTYPE(static void nr_di,(unsigned char *line, int brk));
  49. _PROTOTYPE(static void nr_ds,(unsigned char *line, int brk));
  50. _PROTOTYPE(static void nr_fi,(unsigned char *line, int brk));
  51. _PROTOTYPE(static void nr_fl,(unsigned char *line, int brk));
  52. _PROTOTYPE(static void nr_ft,(unsigned char *line, int brk));
  53. _PROTOTYPE(static void nr_it,(unsigned char *line, int brk));
  54. _PROTOTYPE(static void nr_na,(unsigned char *line, int brk));
  55. _PROTOTYPE(static void nr_nf,(unsigned char *line, int brk));
  56. _PROTOTYPE(static void nr_ns,(unsigned char *line, int brk));
  57. _PROTOTYPE(static void nr_rm,(unsigned char *line, int brk));
  58. _PROTOTYPE(static void nr_rn,(unsigned char *line, int brk));
  59. _PROTOTYPE(static void nr_rr,(unsigned char *line, int brk));
  60. _PROTOTYPE(static void nr_rs,(unsigned char *line, int brk));
  61. _PROTOTYPE(static void nr_tl,(unsigned char *line, int brk));
  62. _PROTOTYPE(static void nr_tm,(unsigned char *line, int brk));
  63. _PROTOTYPE(static void nr_tr,(unsigned char *line, int brk));
  64.  
  65. _PROTOTYPE(static void nr_nil,(unsigned char *line, int brk));
  66.  
  67.  
  68. /*
  69.  * NrReqt[] - nroff request processing table
  70.  *
  71.  * CAUTION: place new entries in their proper alphabetical order, since
  72.  *        this table is processed with a binary search.
  73.  */
  74.  
  75. static struct nr_req {
  76.     char *nm;            /* nroff request */
  77.     void (*fun)();            /* processing function */
  78. } NrReqt[] = {
  79.     { "\\\"",    nr_nil },    /* backslash-quote */
  80.     { "^#",        nr_UL  },
  81.     { "^=",        nr_UL  },
  82.     { "^b",        nr_Ub  },
  83.     { "^c",        nr_Uc  },
  84.     { "^d",        nr_nil },
  85.     { "^e",        nr_nil },
  86.     { "^f",        nr_Uf  },
  87.     { "^r",        nr_Ur  },
  88.     { "^x",        nr_nil },
  89.     { "ad",        nr_ad  },
  90.     { "bp",        nr_bp  },
  91.     { "br",        nr_br  },
  92.     { "ce",        nr_ce  },
  93.     { "di",        nr_di  },
  94.     { "ds",        nr_ds  },
  95.     { "fi",        nr_fi  },
  96.     { "fl",        nr_fl  },
  97.     { "ft",        nr_ft  },
  98.     { "i0",        nr_nil },
  99.     { "it",        nr_it  },
  100.     { "lg",        nr_nil },
  101.     { "li",        nr_nil },
  102.     { "na",        nr_na  },
  103.     { "nf",        nr_nf  },
  104.     { "ns",        nr_ns  },
  105.     { "ps",        nr_nil },
  106.     { "rm",        nr_rm  },
  107.     { "rn",        nr_rn  },
  108.     { "rr",        nr_rr  },
  109.     { "rs",        nr_rs  },
  110.     { "tl",        nr_tl  },
  111.     { "tm",        nr_tm  },
  112.     { "tr",        nr_tr  },
  113.     { "vs",        nr_nil },
  114. };
  115. /*
  116.  * Nreq(line, brk) - process miscellaneous nroff requests from line
  117.  *             buffer with request status of (brk)
  118.  */
  119.  
  120. void
  121. Nreq(line, brk)
  122.     unsigned char *line;
  123.     int brk;
  124. {
  125.     unsigned char c[3];        /* command buffer */
  126.     int cmp, hi, low, mid;        /* binary search indixes */
  127.  
  128.     c[0] = c[1] = c[2] = '\0';
  129.     if ((c[0] = line[1]) != '\0')
  130.         c[1] = line[2];
  131.  
  132.     low = mid = 0;
  133.     hi = (sizeof(NrReqt) / sizeof(struct nr_req)) - 1;
  134.     while (low <= hi) {
  135.         mid = (low + hi) / 2;
  136.         if ((cmp = strcmp((char *)c, NrReqt[mid].nm)) < 0)
  137.             hi = mid - 1;
  138.         else if (cmp > 0)
  139.             low = mid + 1;
  140.         else {
  141.             (void) (*NrReqt[mid].fun)(line, brk);
  142.             return;
  143.         }
  144.     }
  145.     /*
  146.      * Unknown request starting with a '.' or '\''..
  147.      */
  148.     Error(WARN, LINE, " unknown request", NULL);
  149. }
  150.  
  151.  
  152. /*
  153.  * Adjust - "^[.']ad"
  154.  */
  155.  
  156. static void
  157. nr_ad(line, brk)
  158.     unsigned char *line;
  159.     int brk;
  160. {
  161.     Pass3(NOBREAK, (unsigned char *)"both", -1, NULL, 0);
  162. }
  163.  
  164.  
  165. /*
  166. * Begin new page - "^[.']bp"
  167. */
  168.  
  169. static void
  170. nr_bp(line, brk)
  171.     unsigned char *line;
  172.     int brk;
  173. {
  174.     Pass3(brk, (unsigned char *)"need", -1, NULL, 999);
  175. }
  176.  
  177.  
  178. /*
  179. * Break - "^[.']br"
  180. */
  181.  
  182. static void
  183. nr_br(line, brk)
  184.     unsigned char *line;
  185.     int brk;
  186. {
  187.     Pass3(brk, (unsigned char *)"flush", -1, NULL, 0);
  188. }
  189.  
  190.  
  191. /*
  192.  * Center - "^[.']ce"
  193.  */
  194.  
  195. static void
  196. nr_ce(line, brk)
  197.     unsigned char *line;
  198.     int brk;
  199. {
  200.     unsigned char *s;            /* string poiner */
  201.  
  202.     if ((s = Field(2, line, 0)) != NULL)
  203.         Centering = atoi((char *)s);
  204.     else
  205.         Centering = 1;
  206. }
  207.  
  208.  
  209. /*
  210.  * Diversion on and off - "^[.']di"
  211.  */
  212.  
  213. static void
  214. nr_di(line, brk)
  215.     unsigned char *line;
  216.     int brk;
  217. {
  218.     Pass3(DOBREAK, (unsigned char *)"flush", -1, NULL, 0);
  219.     Divert ^= 1;
  220. }
  221.  
  222.  
  223. /*
  224.  * Define string - "^[.']ds"
  225.  */
  226.  
  227. static void
  228. nr_ds(line, brk)
  229.     unsigned char *line;
  230.     int brk;
  231. {
  232.     unsigned char buf[MAXLINE];    /* temporary buffer */
  233.     unsigned char nm[4], nm1[4];    /* name buffers */
  234.     unsigned char *s1, *s2, *s3,    /* temporary string pointers */
  235.                *s4, *s5;
  236.  
  237.     if (Asmname(&line[3], nm) == 0) {
  238.         Error(WARN, LINE, " no name", NULL);
  239.         return;
  240.     }
  241.     if ((s4 = Field(3, line, 0)) == NULL)
  242.         s4 = (unsigned char *)"";
  243. /*
  244.  * Convert "\\b" to '\b' and "\\\\" to '\\'.  Pass all other sequences,
  245.  * beginning with '\\', unmodified.
  246.  */
  247.     s1 = buf;
  248.     while ((s5 = (unsigned char *)strchr((char *)s4, '\\')) != NULL) {
  249.         while (s5 > s4)
  250.             *s1++ = *s4++;
  251.         s4 = ++s5;
  252.         if (*s5 == '\\')
  253.             *s1++ = '\\';
  254.         else if (*s5 == 'b')
  255.             *s1++ = '\b';
  256.         else {
  257.             *s1++ = '\\';
  258.             *s1++ = *s5;
  259.         }
  260.         if (*s4)
  261.             s4++;
  262.     }
  263.     while (*s1++ = *s4++)
  264.         ;
  265. /*
  266.  * Install the string and see if it's an indirect reference -- e.g.,
  267.  *
  268.  *    .ds S1 \*(S2
  269.  */
  270.     s2 = Findstr(nm, buf, 1);
  271.     while (*s2 == '\\' && *(s2 + 1) == '*') {
  272.         s2++;
  273.         s3 = Asmcode(&s2, nm1);
  274.         s2 = Findstr(nm1, NULL, 0);
  275.     }
  276.     if (Hdft) {
  277.  
  278. /*
  279.  * Look for names LH, LF, CH, CF, RH, RF.
  280.  */
  281.         if ((nm[0]=='L' || nm[0]=='C' || nm[0]=='R')
  282.         &&  (nm[1]=='F' || nm[1]=='H')) {
  283.             (void) sprintf((char *)buf, "%s", (char *)nm);
  284.             Pass3(NOBREAK, buf, -1, s2, 0);
  285.         }
  286.     }
  287. }
  288.  
  289.  
  290.  
  291. /*
  292.  * Fill - "^[.']fi"
  293.  */
  294.  
  295. static void
  296. nr_fi(line, brk)
  297.     unsigned char *line;
  298.     int brk;
  299. {
  300.     Fill = 1;
  301.     Pass3(brk, (unsigned char *)"flush", -1, NULL, 0);
  302. }
  303.  
  304.  
  305. /*
  306.  * Flush - "^[.']fl"
  307.  */
  308.  
  309. static void
  310. nr_fl(line, brk)
  311.     unsigned char *line;
  312.     int brk;
  313. {
  314.     Pass3(brk, (unsigned char *)"flush", -1, NULL, 0);
  315. }
  316.  
  317.  
  318. /*
  319.  * Font - "^[.']ft <font_name>"
  320.  */
  321.  
  322. static void
  323. nr_ft(line, brk)
  324.     unsigned char *line;
  325.     int brk;
  326. {
  327.     int i;                /* temporary index */
  328.  
  329.     if (line[3] == '\0' || line[4] == '\0')
  330.         line[4] = 'P';
  331.     if (line[4] == 'P') {
  332.         Font[0] = Prevfont;
  333.         return;
  334.     }
  335.     for (i = 0; Fcode[i].nm; i++) {
  336.         if (Fcode[i].nm == line[4])
  337.         break;
  338.     }
  339.     if (Fcode[i].status == '\0') {
  340.         Error(WARN, LINE, " bad font code", NULL);
  341.         return;
  342.     }
  343.     Prevfont = Font[0];
  344.     Font[0] = line[4];
  345. }
  346.  
  347.  
  348. /*
  349.  * Input trap - "^[.']it [1 <request>]"
  350.  */
  351.  
  352. static void
  353. nr_it(line, brk)
  354.     unsigned char *line;
  355.     int brk;
  356.  
  357. {
  358.     unsigned char buf[MAXLINE];    /* temporary buffer */
  359.     int i;                /* temporary index */
  360.     unsigned char *s1, *s2;        /* temporary string pointers */
  361.  
  362.     if ((s1 = Field(2, line, 0))